package com.aistar.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    private final RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    public RedisUtil(RedisTemplate<String, Serializable> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private static RedisTemplate<String, Serializable> redisTemp;

    @PostConstruct
    void init() {
        redisTemp = this.redisTemplate;
    }

    public static boolean hasKey(String key) {
        Boolean flag = redisTemp.hasKey(key);
        return flag != null
                ? flag
                : false;
    }

    //    ===================string=======================
    public static boolean set(String key, Serializable value) {
        try {
            redisTemp.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean set(String key, Serializable value, long time) {
        if (time <= 0)
            return set(key, value);
        try {
            redisTemp.opsForValue().set(key, value, time, TimeUnit.MILLISECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Serializable get(String key) {
        return key == null
                ? null
                : redisTemp.opsForValue().get(key);
    }

    public static boolean del(String key) {
        if (key == null)
            return false;
        Boolean flag = redisTemp.delete(key);
        return flag != null
                ? flag
                : false;
    }

    //    ===================hash=======================
    public static boolean hset(String key, String field, Serializable value) {
        try {
            redisTemp.opsForHash().put(key, field, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean hset(String key, Map<String, Serializable> map) {
        try {
            redisTemp.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Serializable hget(String key, String field) {
        return key == null
                ? null
                : (Serializable) redisTemp.opsForHash().get(key, field);
    }

    public static Map<Object, Object> hgetAll(String key) {
        return key == null
                ? null
                : redisTemp.opsForHash().entries(key);
    }

    public static Long hdel(String key, String... field) {
        if (key == null || field == null || field.length == 0)
            return null;
        return redisTemp.opsForHash().delete(key, (Object[]) field);
    }

    //    ===================set=======================
    public static Long sadd(String key, Serializable... values) {
        try {
            return redisTemp.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Set<Serializable> smembers(String key) {
        return key == null
                ? null
                : redisTemp.opsForSet().members(key);
    }

    public static Long srem(String key, Serializable... values) {
        if (key == null)
            return null;
        return redisTemp.opsForSet().remove(key, (Object[]) values);
    }

    //    ===================list=======================
    public static boolean lpush(String key, Serializable... values) {
        try {
            redisTemp.opsForList().leftPush(key, values);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean rpush(String key, Serializable... values) {
        try {
            redisTemp.opsForList().rightPush(key, values);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static List<Serializable> lrange(String key, long start, long end) {
        try {
            return redisTemp.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Serializable lpop(String key) {
        try {
            return redisTemp.opsForList().leftPop(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Serializable rpop(String key) {
        try {
            return redisTemp.opsForList().rightPop(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
